<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2016/8/11
 * Time: 上午11:10
 */

namespace Home\Model;
use Think\Model;
/**
 * CREATE TABLE `hospital_user_score_log` (
 * `user_id` INT UNSIGNED NOT NULL,
 * `create_time` INT UNSIGNED NOT NULL)
 * DEFAULT CHARACTER SET = utf8;
 * Class UserScoreLogModel : 记录用户每次评论的时间，防止不停的 post，隔天会清空
 * @package Home\Model
 */
class UserScoreLogModel extends Model
{
    protected $tableName = 'user_score_log';

    public static function insertLog($userID)
    {
        $data = array(
            'user_id' => $userID,
            'create_time' => time(),
        );
        $object = new static();
        return $object->add($data);
    }

    public static function clear()
    {
        $time = strtotime(date('Y-m-d 00:00:00',time()));
        $condition = array(
            'create_time' => array(
                'lt' , $time
            )
        );
        $object = new static();
        return $object->where($condition)->delete();
    }

    public static function isAllow($userID)
    {
        $result = true;
        $object = new static();
        $count = $object->where(array('user_id' => $userID , 'create_time' => array('gt' , (time() - 60))))->count('*');
        $totalCount = $object->where(array('user_id' => $userID))->count('*');
        if($count > 20 || $totalCount > 500)
        {
            $result = false;
        }
        return $result;
    }

    public static function userExists($userID)
    {
        $result = false;
        $user = M('common_member','ivf_','BBS_DB_CONFIG')->where(array('uid' => (int)$userID))->field('uid')->find();
        if(isset($user['uid']))
        {
            $result = true;
        }
        return $result;
    }

    public static function getVidByUserID($userID)
    {
        $sql = 'select * from (SELECT * FROM ivf_forum_post where first = 1 and authorid = '. $userID .' order by pid desc) as a limit 0,1';
        $user = M('common_member','ivf_','BBS_DB_CONFIG')->query($sql);
        $vid = null;
        if($user && isset($user[0]['tid']))
        {
            $vid = $user[0]['tid'];
        }
        return $vid;
    }
}